home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ue312src.zip / FILE.C < prev    next >
C/C++ Source or Header  |  1993-04-21  |  22KB  |  875 lines

  1. /*    FILE.C:   for MicroEMACS
  2.  
  3.     The routines in this file handle the reading, writing
  4.     and lookup of disk files.  All of details about the
  5.     reading and writing of the disk are in "fileio.c".
  6.  
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14. #if    BSD | SUN | V7
  15. #include    <sys/types.h>
  16. #include    <sys/stat.h>
  17. #endif
  18.  
  19. /*
  20.  * Read a file into the current
  21.  * buffer. This is really easy; all you do is
  22.  * find the name of the file, and call the standard
  23.  * "read a file into the current buffer" code.
  24.  * Bound to "C-X C-R".
  25.  */
  26. PASCAL NEAR fileread(f, n)
  27.  
  28. int f, n;    /* defualt and numeric arguments (unused) */
  29.  
  30. {
  31.     char *fname;    /* file name to read */
  32.  
  33.     if (restflag)        /* don't allow this command if restricted */
  34.         return(resterr());
  35.  
  36.     if ((fname = gtfilename(TEXT131)) == NULL)
  37. /*                              "Read file" */
  38.         return(FALSE);
  39.     return(readin(fname, TRUE));
  40. }
  41.  
  42. /*
  43.  * Insert a file into the current
  44.  * buffer. This is really easy; all you do it
  45.  * find the name of the file, and call the standard
  46.  * "insert a file into the current buffer" code.
  47.  * Bound to "C-X C-I".
  48.  */
  49. PASCAL NEAR insfile(f, n)
  50.  
  51. int f,n;    /* prefix flag and argument */
  52.  
  53. {
  54.     register int    s;
  55.     char *fname;    /* file name */
  56.     LINE *curline;
  57.  
  58.     if (restflag)        /* don't allow this command if restricted */
  59.         return(resterr());
  60.     if (curbp->b_mode&MDVIEW)      /* don't allow this command if  */
  61.         return(rdonly());    /* we are in read only mode    */
  62.  
  63.     if ((fname = gtfilename(TEXT132)) == NULL) 
  64. /*                              "Insert file" */
  65.         return(FALSE);
  66.     /*
  67.      * Save the local pointers to hold global ".", in case
  68.      * $yankflag is set to 1.  Insert-file always places the
  69.      * starting offset point at 0.  Hold *previous* line
  70.      * position, since the current line may be re-allocated.
  71.      */
  72.     if (yankflag)
  73.         curline = lback(curwp->w_dotp);
  74.  
  75.     s = ifile(fname);
  76.  
  77.     if (yankflag)
  78.         curwp->w_dotp = lforw(curline);
  79.  
  80.     return (s);
  81. }
  82.  
  83. /*
  84.  * Select a file for editing.
  85.  * Look around to see if you can find the
  86.  * fine in another buffer; if you can find it
  87.  * just switch to the buffer. If you cannot find
  88.  * the file, create a new buffer, read in the
  89.  * text, and switch to the new buffer.
  90.  * Bound to C-X C-F.
  91.  */
  92. PASCAL NEAR filefind(f, n)
  93.  
  94. int f,n;    /* prefix flag and argument */
  95.  
  96. {
  97.     char *fname;    /* file user wishes to find */    /* file name */
  98.     register int s;        /* status return */
  99.  
  100.     if (restflag)        /* don't allow this command if restricted */
  101.         return(resterr());
  102.  
  103.     if ((fname = gtfilename(TEXT133)) == NULL) 
  104. /*                              "Find file" */
  105.         return(FALSE);
  106.     return(getfile(fname, TRUE));
  107. }
  108.  
  109. PASCAL NEAR viewfile(f, n)    /* visit a file in VIEW mode */
  110.  
  111. int f,n;    /* prefix flag and argument */
  112.  
  113. {
  114.     char *fname;    /* file user wishes to find */    /* file name */
  115.     register int s;    /* status return */
  116.  
  117.     if (restflag)        /* don't allow this command if restricted */
  118.         return(resterr());
  119.  
  120.     if ((fname = gtfilename(TEXT134)) == NULL) 
  121. /*                              "View file" */
  122.         return(FALSE);
  123.     s = getfile(fname, FALSE);
  124.     if (s) {    /* if we succeed, put it in view mode */
  125.         curwp->w_bufp->b_mode |= MDVIEW;
  126.         upmode();
  127.     }
  128.     return(s);
  129. }
  130.  
  131. #if    CRYPT
  132. PASCAL NEAR resetkey()    /* reset the encryption key if needed */
  133.  
  134. {
  135.     register int s; /* return status */
  136.  
  137.     /* turn off the encryption flag */
  138.     cryptflag = FALSE;
  139.  
  140.     /* if we are in crypt mode */
  141.     if (curbp->b_mode & MDCRYPT) {
  142.         if (curbp->b_key[0] == 0) {
  143.             s = setekey(FALSE, 0);
  144.             if (s != TRUE)
  145.                 return(s);
  146.         }
  147.  
  148.         /* let others know... */
  149.         cryptflag = TRUE;
  150.  
  151.         /* and set up the key to be used! */
  152.         /* de-encrypt it */
  153.         crypt((char *)NULL, 0);
  154.         crypt(curbp->b_key, strlen(curbp->b_key));
  155.  
  156.         /* re-encrypt it...seeding it to start */
  157.         crypt((char *)NULL, 0);
  158.         crypt(curbp->b_key, strlen(curbp->b_key));
  159.     }
  160.  
  161.     return(TRUE);
  162. }
  163. #endif
  164.  
  165. PASCAL NEAR getfile(fname, lockfl)
  166.  
  167. char fname[];        /* file name to find */
  168. int lockfl;        /* check the file for locks? */
  169.  
  170. {
  171.     register BUFFER *bp;
  172.     register LINE    *lp;
  173.     register int    i;
  174.     register int    s;
  175.     register int cmark;    /* current mark */
  176.     char bname[NBUFN];    /* buffer name to put file */
  177.     char prompt[NSTRING];    /* string for collisions prompt */
  178.  
  179. #if    MSDOS | WINNT | AOSVS | VMS | TOS
  180.     mklower(fname);            /* msdos isn't case sensitive */
  181. #endif
  182.     for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  183.         if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  184.             swbuffer(bp);
  185.             lp = curwp->w_dotp;
  186.             i = curwp->w_ntrows/2;
  187.             while (i-- && lback(lp)!=curbp->b_linep)
  188.                 lp = lback(lp);
  189.             curwp->w_linep = lp;
  190.             curwp->w_flag |= WFMODE|WFHARD;
  191.             mlwrite(TEXT135);
  192. /*                              "[Old buffer]" */
  193.             return(TRUE);
  194.         }
  195.     }
  196.     makename(bname, fname);         /* New buffer name.    */
  197.  
  198.     /* prevent buffer name conflicts */
  199.     while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  200.  
  201.         /* first, come up with our own name */
  202.         unqname(bname);
  203.  
  204.         /* if interactive, let em change it if they dislike our names */
  205.         if (clexec == FALSE) {
  206.  
  207.             strcpy(prompt, TEXT136);
  208. /*                       "Buffer name: " */
  209.             strcpy(&prompt[strlen(prompt) - 2], "[");
  210.             strcat(prompt, bname);
  211.             strcat(prompt, "]: ");
  212.             s = mlreply(prompt, bname, NBUFN);
  213.  
  214.             if (s == ABORT)     /* ^G to just quit    */
  215.                 return(s);
  216.             if (s == FALSE) {    /* CR to let the computer pick */
  217.                 makename(bname, fname);    /* New buffer name. */
  218.                 unqname(bname);        /* which is unique */
  219.             }
  220.         }
  221.     }
  222.  
  223.     /* create the new buffer */
  224.     if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  225.         mlwrite(TEXT137);
  226. /*                      "Cannot create buffer" */
  227.         return(FALSE);
  228.     }
  229.  
  230.     swbuffer(bp);            /* switch to the new buffer */
  231.     return(readin(fname, lockfl));    /* Read it in.        */
  232. }
  233.  
  234. /*
  235.     Read file "fname" into the current buffer, blowing away any text
  236.     found there.  Called by both the read and find commands.  Return
  237.     the final status of the read.  Also called by the mainline, to
  238.     read in a file specified on the command line as an argument. 
  239.     The command in $readhook is called after the buffer is set up
  240.     and before it is read. 
  241. */
  242.  
  243. PASCAL NEAR readin(fname, lockfl)
  244.  
  245. char    fname[];    /* name of file to read */
  246. int    lockfl;        /* check for file locks? */
  247.  
  248. {
  249.     register LINE *lp1;
  250.     register LINE *lp2;
  251.     register int i;
  252.     register WINDOW *wp;
  253.     register BUFFER *bp;
  254.     register int s;
  255.     register int nline;
  256.     register int cmark;    /* current mark */
  257.     int nbytes;
  258.     char mesg[NSTRING];
  259.  
  260. #if    FILOCK
  261.     if (lockfl && lockchk(fname) == ABORT)
  262.         return(ABORT);
  263. #endif
  264.  
  265.     bp = curbp;                /* Cheap.        */
  266.     if ((s=bclear(bp)) != TRUE)        /* Might be old.    */
  267.         return(s);
  268.     bp->b_flag &= ~(BFINVS|BFCHG);
  269.     strcpy(bp->b_fname, fname);
  270.  
  271.     /* let a user macro get hold of things...if he wants */
  272.     execkey(&readhook, FALSE, 1);
  273.  
  274. #if    CRYPT
  275.     /* set up for decryption */
  276.     s = resetkey();
  277.     if (s != TRUE)
  278.         return(s);
  279. #endif
  280.  
  281.     /* turn off ALL keyboard translation in case we get a dos error */
  282.     TTkclose();
  283.  
  284.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  285.         goto out;
  286.  
  287.     if (s == FIOFNF) {            /* File not found.    */
  288.         mlwrite(TEXT138);
  289. /*                      "[New file]" */
  290.         goto out;
  291.     }
  292.  
  293.     /* read the file in */
  294.     mlwrite(TEXT139);
  295. /*              "[Reading file]" */
  296.     nline = 0;
  297.     while ((s=ffgetline(&nbytes)) == FIOSUC) {
  298.         if ((lp1=lalloc(nbytes)) == NULL) {
  299.             s = FIOMEM;        /* Keep message on the    */
  300.             break;            /* display.        */
  301.         }
  302.         lp2 = lback(curbp->b_linep);
  303.         lp2->l_fp = lp1;
  304.         lp1->l_fp = curbp->b_linep;
  305.         lp1->l_bp = lp2;
  306.         curbp->b_linep->l_bp = lp1;
  307.         for (i=0; i<nbytes; ++i)
  308.             lputc(lp1, i, fline[i]);
  309.         ++nline;
  310.     }
  311.     ffclose();                /* Ignore errors.    */
  312.     strcpy(mesg, "[");
  313.     if (s==FIOERR) {
  314.         strcat(mesg, TEXT141);
  315. /*                           "I/O ERROR, " */
  316.         curbp->b_flag |= BFTRUNC;
  317.     }
  318.     if (s == FIOMEM) {
  319.         strcat(mesg, TEXT142);
  320. /*                           "OUT OF MEMORY, " */
  321.         curbp->b_flag |= BFTRUNC;
  322.     }
  323.     strcat(mesg, TEXT140);
  324. /*                   "Read " */
  325.     strcat(mesg, int_asc(nline));
  326.     strcat(mesg, TEXT143);
  327. /*                   " line" */
  328.     if (nline > 1)
  329.         strcat(mesg, "s");
  330.     strcat(mesg, "]");
  331.     mlwrite(mesg);
  332.  
  333. out:
  334.     TTkopen();    /* open the keyboard again */
  335.     for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  336.         if (wp->w_bufp == curbp) {
  337.             wp->w_linep = lforw(curbp->b_linep);
  338.             wp->w_dotp  = lforw(curbp->b_linep);
  339.             wp->w_doto  = 0;
  340.             for (cmark = 0; cmark < NMARKS; cmark++) {
  341.                 wp->w_markp[cmark] = NULL;
  342.                 wp->w_marko[cmark] = 0;
  343.             }
  344.             wp->w_flag |= WFMODE|WFHARD;
  345.         }
  346.     }
  347.     if (s == FIOERR || s == FIOFNF)     /* False if error.    */
  348.         return(FALSE);
  349.     return(TRUE);
  350. }
  351.  
  352. /*
  353.  * Take a file name, and from it
  354.  * fabricate a buffer name. This routine knows
  355.  * about the syntax of file names on the target system.
  356.  * I suppose that this information could be put in
  357.  * a better place than a line of code.
  358.  * Returns a pointer into fname indicating the end of the file path; i.e.,
  359.  * 1 character BEYOND the path name.
  360.  */
  361. char *PASCAL NEAR makename(bname, fname)
  362.  
  363. char *bname;
  364. char *fname;
  365.  
  366. {
  367.     register char *cp1;
  368.     register char *cp2;
  369.     register char *pathp;
  370.  
  371. #if     AOSVS | MV_UX
  372.         resolve_full_pathname(fname, fname);
  373.         mklower(fname);   /* aos/vs not case sensitive */
  374. #endif
  375.     cp1 = &fname[0];
  376.     while (*cp1 != 0)
  377.         ++cp1;
  378.  
  379. #if    AMIGA
  380.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  381.         --cp1;
  382. #endif
  383. #if     AOSVS | MV_UX
  384.         while (cp1!=&fname[0] && cp1[-1]!=':')
  385.                 --cp1;
  386. #endif
  387. #if    VMS
  388.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  389.         --cp1;
  390. #endif
  391. #if    MSDOS | OS2 | WINNT
  392.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  393.         --cp1;
  394. #endif
  395. #if    TOS
  396.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  397.         --cp1;
  398. #endif
  399. #if    FINDER
  400.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  401.         --cp1;
  402. #endif
  403. #if    V7 | USG | AUX | SMOS | HPUX8 | HPUX9 | BSD | SUN | XENIX | AVIION
  404.     while (cp1!=&fname[0] && cp1[-1]!='/')
  405.         --cp1;
  406. #endif
  407. #if WMCS
  408.     while (cp1!=&fname[0] && cp1[-1]!='_' && cp1[-1]!='/')
  409.         --cp1;
  410. #endif
  411.     /* cp1 is pointing to the first real filename char */
  412.     pathp = cp1;
  413.  
  414.     cp2 = &bname[0];
  415.     while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  416.         *cp2++ = *cp1++;
  417.     *cp2 = 0;
  418.  
  419.     return(pathp);
  420. }
  421.  
  422. VOID PASCAL NEAR unqname(name)    /* make sure a buffer name is unique */
  423.  
  424. char *name;    /* name to check on */
  425.  
  426. {
  427.     register char *sp;
  428.  
  429.     /* check to see if it is in the buffer list */
  430.     while (bfind(name, 0, FALSE) != NULL) {
  431.  
  432.         /* go to the end of the name */
  433.         sp = name;
  434.         while (*sp)
  435.             ++sp;
  436.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  437.             *sp++ = '0';
  438.             *sp = 0;
  439.         } else
  440.               *(--sp) += 1;
  441.     }
  442. }
  443.  
  444. /*
  445.  * Ask for a file name, and write the
  446.  * contents of the current buffer to that file.
  447.  * Update the remembered file name and clear the
  448.  * buffer changed flag. This handling of file names
  449.  * is different from the earlier versions, and
  450.  * is more compatable with Gosling EMACS than
  451.  * with ITS EMACS. Bound to "C-X C-W" for writing
  452.  * and ^X^A for appending.
  453.  */
  454.  
  455. PASCAL NEAR filewrite(f, n)
  456.  
  457. int f, n;    /* emacs arguments */
  458.  
  459. {
  460.     register int s;
  461.     char *fname;
  462.  
  463.     if (restflag)        /* don't allow this command if restricted */
  464.         return(resterr());
  465.  
  466.     if ((fname = gtfilename(TEXT144)) == NULL)
  467. /*                     "Write file: " */
  468.         return(FALSE);
  469.     if ((s=writeout(fname, "w")) == TRUE) {
  470.         strcpy(curbp->b_fname, fname);
  471.         curbp->b_flag &= ~BFCHG;
  472.         /* Update mode lines.    */
  473.         upmode();
  474.     }
  475.     return(s);
  476. }
  477.  
  478. PASCAL NEAR fileapp(f, n)    /* append file */
  479.  
  480. int f, n;    /* emacs arguments */
  481.  
  482. {
  483.     register int s;
  484.     char *fname;
  485.  
  486.     if (restflag)        /* don't allow this command if restricted */
  487.         return(resterr());
  488.     if ((fname = gtfilename(TEXT218)) == NULL)
  489. /*                     "Append file: " */
  490.         return(FALSE);
  491.     if ((s=writeout(fname, "a")) == TRUE) {
  492.         curbp->b_flag &= ~BFCHG;
  493.         /* Update mode lines.    */
  494.         upmode();
  495.     }
  496.     return(s);
  497. }
  498.  
  499. /*
  500.  * Save the contents of the current
  501.  * buffer in its associatd file. Do nothing
  502.  * if nothing has changed (this may be a bug, not a
  503.  * feature). Error if there is no remembered file
  504.  * name for the buffer. Bound to "C-X C-S". May
  505.  * get called by "C-Z".
  506.  */
  507. PASCAL NEAR filesave(f, n)
  508.  
  509. int f,n;    /* prefix flag and argument */
  510.  
  511. {
  512.     register int s;
  513.  
  514.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  515.         return(rdonly());    /* we are in read only mode    */
  516.     if ((curbp->b_flag&BFCHG) == 0)     /* Return, no changes.    */
  517.         return(TRUE);
  518.     if (curbp->b_fname[0] == 0) {        /* Must have a name.    */
  519.         mlwrite(TEXT145);
  520. /*                      "No file name" */
  521.         return(FALSE);
  522.     }
  523.  
  524.     /* complain about truncated files */
  525.     if ((curbp->b_flag&BFTRUNC) != 0) {
  526.         if (mlyesno(TEXT146) == FALSE) {
  527. /*                          "Truncated file..write it out" */
  528.             mlwrite(TEXT8);
  529. /*                              "[Aborted]" */
  530.             return(FALSE);
  531.         }
  532.     }
  533.  
  534.     /* complain about narrowed buffers */
  535.     if ((curbp->b_flag&BFNAROW) != 0) {
  536.         if (mlyesno(TEXT147) == FALSE) {
  537. /*                          "Narrowed Buffer..write it out" */
  538.             mlwrite(TEXT8);
  539. /*                              "[Aborted]" */
  540.             return(FALSE);
  541.         }
  542.     }
  543.  
  544.     if ((s=writeout(curbp->b_fname, "w")) == TRUE) {
  545.         curbp->b_flag &= ~BFCHG;
  546.         /* Update mode lines.    */
  547.         upmode();
  548.     }
  549.     return(s);
  550. }
  551.  
  552. /*
  553.  * This function performs the details of file writing. It uses
  554.  * the file management routines in the "fileio.c" package. The
  555.  * number of lines written is displayed. Several errors are
  556.  * posible, and cause writeout to return a FALSE result. When
  557.  * $ssave is TRUE,  the buffer is written out to a temporary
  558.  * file, and then the old file is unlinked and the temporary
  559.  * renamed to the original name.  Before the file is written,
  560.  * a user specifyable routine (in $writehook) can be run.
  561.  */
  562.  
  563. PASCAL NEAR writeout(fn, mode)
  564.  
  565. char *fn;    /* name of file to write current buffer to */
  566. char *mode;    /* mode to open file (w = write a = append) */
  567. {
  568.     register LINE *lp;    /* line to scan while writing */
  569.     register char *sp;    /* temporary string pointer */
  570.     register int nline;    /* number of lines written */
  571.     int status;        /* return status */
  572.     int sflag;        /* are we safe saving? */
  573.     char tname[NSTRING];    /* temporary file name */
  574.     char buf[NSTRING];    /* message buffer */
  575. #if    BSD | SUN | V7 | XENIX
  576.     struct stat st;        /* we need info about the file permisions */
  577. #endif
  578.  
  579.     /* let a user macro get hold of things...if he wants */
  580.     execkey(&writehook, FALSE, 1);
  581.  
  582.     /* determine if we will use the save method */
  583.     sflag = FALSE;
  584.     if (ssave && fexist(fn) && *mode == 'w')
  585.         sflag = TRUE;
  586.  
  587. #if    CRYPT
  588.     /* set up for file encryption */
  589.     status = resetkey();
  590.     if (status != TRUE)
  591.         return(status);
  592. #endif
  593.  
  594.     /* turn off ALL keyboard translation in case we get a dos error */
  595.     TTkclose();
  596.  
  597.     /* Perform Safe Save..... */
  598.     if (sflag) {
  599.         /* duplicate original file name, and find where to trunc it */
  600.         sp = tname + (makename(tname, fn) - fn) + 1;
  601.         strcpy(tname, fn);
  602.  
  603.         /* create a unique name, using random numbers */
  604.         do {
  605.             *sp = 0;
  606.             strcat(tname, int_asc(ernd() & 0xffff));
  607.         } while(fexist(tname));
  608.  
  609.         /* open the temporary file */
  610. #if     AOSVS
  611.                 status = ffwopen(fn, "w", tname);
  612. #else
  613.         status = ffwopen(tname, "w");
  614. #endif
  615.     } else
  616. #if     AOSVS
  617.                 status = ffwopen(fn, mode, NULL);
  618. #else
  619.         status = ffwopen(fn, mode);
  620. #endif
  621.  
  622.     /* if the open failed.. clean up and abort */
  623.     if (status != FIOSUC) {
  624.         TTkopen();
  625.         return(FALSE);
  626.     }
  627.  
  628.     /* write the current buffer's lines to the open disk file */
  629.     mlwrite(TEXT148);    /* tell us that we're writing */
  630. /*              "[Writing...]" */
  631.     lp = lforw(curbp->b_linep);    /* start at the first line.    */
  632.     nline = 0;            /* track the Number of lines    */
  633.     while (lp != curbp->b_linep) {
  634.         if ((status = ffputline(&lp->l_text[0], lused(lp))) != FIOSUC)
  635.             break;
  636.         ++nline;
  637.         lp = lforw(lp);
  638.     }
  639.  
  640.  
  641.     /* report on status of file write */
  642.     *buf = 0;
  643.     status |= ffclose();
  644.     if (status == FIOSUC) {
  645.         /* report on success (or lack therof) */
  646.         strcpy(buf, TEXT149);
  647. /*                          "[Wrote " */
  648.         strcat(buf, int_asc(nline));
  649.         strcat(buf, TEXT143);
  650. /*                          " line" */
  651.         if (nline > 1)
  652.             strcat(buf, "s");
  653.  
  654.         if (sflag) {
  655. #if    BSD | SUN | V7 | XENIX
  656.             /* get the permisions on the original file */
  657.             stat(fn, &st);
  658. #endif
  659.             /* erase original file */
  660.             /* rename temporary file to original name */
  661.             if (unlink(fn) == 0 && rename(tname, fn) == 0) {
  662. #if    BSD | SUN | V7 | XENIX
  663.                 chown(fn, (int)st.st_uid, (int)st.st_gid);
  664.                 chmod(fn, (int)st.st_mode);
  665. #else
  666.                 ;
  667. #endif
  668.             } else {
  669.                 strcat(buf, TEXT150);
  670. /*                                          ", saved as " */
  671.                 strcat(buf, tname);
  672.                 status = FIODEL;        /* failed */
  673.             }
  674.         }
  675.         strcat(buf, "]");
  676.         mlwrite(buf);
  677.     }
  678.  
  679.     /* reopen the keyboard, and return our status */
  680.     TTkopen();
  681.     return(status == FIOSUC);
  682. }
  683.  
  684. /*
  685.  * The command allows the user
  686.  * to modify the file name associated with
  687.  * the current buffer. It is like the "f" command
  688.  * in UNIX "ed". The operation is simple; just zap
  689.  * the name in the BUFFER structure, and mark the windows
  690.  * as needing an update. You can type a blank line at the
  691.  * prompt if you wish.
  692.  */
  693.  
  694. PASCAL NEAR filename(f, n)
  695.  
  696. int f,n;    /* prefix flag and argument */
  697.  
  698. {
  699.     register int    s;
  700.     char        fname[NFILEN];
  701.  
  702.     if (restflag)        /* don't allow this command if restricted */
  703.         return(resterr());
  704.     if ((s=FILENAMEREPLY(TEXT151, fname, NFILEN)) == ABORT)
  705. /*                     "Name: " */
  706.         return(s);
  707.     if (s == FALSE)
  708.         strcpy(curbp->b_fname, "");
  709.     else
  710.         strcpy(curbp->b_fname, fname);
  711.     /* Update mode lines.    */
  712.     upmode();
  713.     curbp->b_mode &= ~MDVIEW;      /* no longer read only mode */
  714.     return(TRUE);
  715. }
  716.  
  717. /*
  718.  * Insert file "fname" into the current
  719.  * buffer, Called by insert file command. Return the final
  720.  * status of the read.
  721.  */
  722. PASCAL NEAR ifile(fname)
  723. char    fname[];
  724. {
  725.     register LINE *lp0;
  726.     register LINE *lp1;
  727.     register LINE *lp2;
  728.     register int i;
  729.     register BUFFER *bp;
  730.     register int s;
  731.     register int nline;
  732.     int nbytes;
  733.     int cmark;    /* current mark */
  734.     char mesg[NSTRING];
  735.  
  736.     bp = curbp;                /* Cheap.        */
  737.     bp->b_flag |= BFCHG;            /* we have changed    */
  738.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  739.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  740.         goto out;
  741.     if (s == FIOFNF) {            /* File not found.    */
  742.         mlwrite(TEXT152);
  743. /*                      "[No such file]" */
  744.         return(FALSE);
  745.     }
  746.     mlwrite(TEXT153);
  747. /*              "[Inserting file]" */
  748.  
  749. #if    CRYPT
  750.     s = resetkey();
  751.     if (s != TRUE)
  752.         return(s);
  753. #endif
  754.     /* back up a line and save the mark here */
  755.     curwp->w_dotp = lback(curwp->w_dotp);
  756.     curwp->w_doto = 0;
  757.     for (cmark = 0; cmark < NMARKS; cmark++) {
  758.         curwp->w_markp[cmark] = curwp->w_dotp;
  759.         curwp->w_marko[cmark] = 0;
  760.     }
  761.  
  762.     nline = 0;
  763.     while ((s=ffgetline(&nbytes)) == FIOSUC) {
  764.         if ((lp1=lalloc(nbytes)) == NULL) {
  765.             s = FIOMEM;        /* Keep message on the    */
  766.             break;            /* display.        */
  767.         }
  768.         lp0 = curwp->w_dotp;  /* line previous to insert */
  769.         lp2 = lp0->l_fp;    /* line after insert */
  770.  
  771.         /* re-link new line between lp0 and lp2 */
  772.         lp2->l_bp = lp1;
  773.         lp0->l_fp = lp1;
  774.         lp1->l_bp = lp0;
  775.         lp1->l_fp = lp2;
  776.  
  777.         /* and advance and write out the current line */
  778.         curwp->w_dotp = lp1;
  779.         for (i=0; i<nbytes; ++i)
  780.             lputc(lp1, i, fline[i]);
  781.         ++nline;
  782.     }
  783.     ffclose();                /* Ignore errors.    */
  784.     curwp->w_markp[0] = lforw(curwp->w_markp[0]);
  785.     strcpy(mesg, "[");
  786.     if (s==FIOERR) {
  787.         strcat(mesg, TEXT141);
  788. /*                           "I/O ERROR, " */
  789.         curbp->b_flag |= BFTRUNC;
  790.     }
  791.     if (s == FIOMEM) {
  792.         strcat(mesg, TEXT142);
  793. /*                           "OUT OF MEMORY, " */
  794.         curbp->b_flag |= BFTRUNC;
  795.     }
  796.     strcat(mesg, TEXT154);
  797. /*                   "Inserted " */
  798.     strcat(mesg, int_asc(nline));
  799.     strcat(mesg, TEXT143);
  800. /*                   " line" */
  801.     if (nline > 1)
  802.         strcat(mesg, "s");
  803.     strcat(mesg, "]");
  804.     mlwrite(mesg);
  805.  
  806. out:
  807.     /* advance to the next line and mark the window for changes */
  808.     curwp->w_dotp = lforw(curwp->w_dotp);
  809.     curwp->w_flag |= WFHARD | WFMODE;
  810.  
  811.     /* copy window parameters back to the buffer structure */
  812.     curbp->b_dotp = curwp->w_dotp;
  813.     curbp->b_doto = curwp->w_doto;
  814.     for (cmark = 0; cmark < NMARKS; cmark++) {
  815.         curbp->b_markp[cmark] = curwp->w_markp[cmark];
  816.         curbp->b_marko[cmark] = curwp->w_marko[cmark];
  817.     }
  818.     curbp->b_fcol = curwp->w_fcol;
  819.  
  820.     if (s == FIOERR)            /* False if error.    */
  821.         return(FALSE);
  822.     return(TRUE);
  823. }
  824.  
  825. /*    show-files    Bring up a fake buffer and list the
  826.             names of all the files in a given directory
  827. */
  828.  
  829. PASCAL NEAR showfiles(f, n)
  830.  
  831. int f,n;    /* prefix flag and argument */
  832.  
  833. {
  834.     register BUFFER *dirbuf;/* buffer to put file list into */
  835.     char outseq[NSTRING];    /* output buffer for file names */
  836.     char *sp;        /* output ptr for file names */
  837.     char mstring[NSTRING];    /* string to match cmd names to */
  838.     int status;        /* status return */
  839.  
  840.     /* ask what directory mask to search */
  841.     status = mlreply("Directory to show: ", mstring, NSTRING - 1);
  842.     if (status == ABORT)
  843.         return(status);
  844.  
  845.     /* get a buffer for the file list */
  846.     dirbuf = bfind("File List", TRUE, BFINVS);
  847.     if (dirbuf == NULL || bclear(dirbuf) == FALSE) {
  848.         mlwrite("Can not display file list");
  849. /*            "Can not display function list" */
  850.         return(FALSE);
  851.     }
  852.  
  853.     /* let us know this is in progress */
  854.     mlwrite("[Building File List]");
  855.  
  856.     /* get the first file name */
  857.     sp = getffile(mstring);
  858.  
  859.     while (sp) {
  860.  
  861.         /* add a name to the buffer */
  862.         strcpy(outseq, sp);
  863.         if (addline(dirbuf, outseq) != TRUE)
  864.             return(FALSE);
  865.  
  866.         /* and get the next name */
  867.         sp = getnfile();
  868.     }
  869.  
  870.     /* display the list */
  871.     wpopup(dirbuf);
  872.     mlerase();    /* clear the mode line */
  873.     return(TRUE);
  874. }
  875.